package org.cloud.controller;


import com.netflix.discovery.DiscoveryManager;
import org.cloud.entity.Dept;
import org.cloud.service.DeptService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private DeptService deptService;

    @GetMapping("/discovery/{serviceId}")
    public Object discovery(@PathVariable("serviceId") String serviceId) {
        List<String> list = discoveryClient.getServices();
        System.out.println(" 服务名称列表: " + list);
        List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceId);
        for (ServiceInstance instance : instanceList) {
            System.out.println(" 服务 ID: " + instance.getInstanceId() + " 主机: " + instance.getHost());
            System.out.println(" 端口: " + instance.getPort() + " URI: " + instance.getUri());
        }
        return discoveryClient;
    }

    @GetMapping("/list")
    public List<Dept> ListDept() {
        return deptService.listDept();
    }

    @GetMapping("/get/{id}")
    public Dept getDept(@PathVariable("id") Integer id) {
        return deptService.getDept(id);
    }

    @PostMapping("/add")
    public boolean addDept(@RequestBody Dept dept) {
         return deptService.addDept(dept);
    }


    @GetMapping("/offline")
    // 下线服务
    public void offLine(){
        DiscoveryManager.getInstance().shutdownComponent();
    }
}
